use crate::core::model::data::Data;
use std::collections::HashMap;

/// ### important
/// std 方法的定义
pub type StdFunc = Box<dyn Fn(Vec<Data>) -> Option<Data> + Send + Sync>;

/// ### important
/// 将STD方法初始化入函数表的方法
pub fn init_std_fn() -> HashMap<String, StdFunc> {
    let mut map = HashMap::<String, StdFunc>::new();
    map.insert("puts".to_string(), Box::new(puts_));
    map.insert("fmt".to_string(), Box::new(fmt_));
    map
}

/// 标准输出
pub fn puts_(param: Vec<Data>) -> Option<Data> {
    for data in param {
        println!("{}", data);
    }
    None
}

#[test]
fn t_puts() {
    let v = vec![Data::String("hi".to_string())];
    let r = puts_(v);
    println!("return : {:?}", r);
}

/// 格式化
pub fn fmt_(param: Vec<Data>) -> Option<Data> {
    let mut s = String::new();
    for data in param {
        s = format!("{}{}", s, data);
    }
    Some(Data::String(s))
}

#[test]
fn t_fmt() {
    let v = vec![Data::String("hi".to_string()), Data::Num(713)];
    let r = fmt_(v);
    println!("return : {:?}", r);
}